<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px">
      <el-form-item label="活动名称" prop="name">
        <el-input maxlength="15" placeholder="活动名称" v-model="dataForm.name"></el-input>
      </el-form-item>
      <el-form-item label="起止时间" class="date-lable" prop='start_time'>
        <el-form-item prop="start_time" label-width="0px">
          <el-date-picker v-model="dataForm.start_time" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <span style="padding: 0 20px">——</span>
        <el-form-item prop="end_time" label-width="0px">
          <el-date-picker v-model="dataForm.end_time" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
      </el-form-item>
      <el-form-item label="活动图片" prop="img_url">
        <Imglist @updataImg="updataImg" :value="imgArr" :dialogVisible="visible" :tip="'* 尺寸建议750x268'" :width="750" :height="268"></Imglist>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dataForm.img_url" alt="" />
        </el-dialog>
      </el-form-item>
      <el-form-item label="活动说明" prop="explain">
        <el-input type="textarea" v-model="dataForm.explain" placeholder="" :maxlength="200"></el-input>
      </el-form-item>
      <el-form-item label="订单自动关闭时间" prop="close_time">
        <el-input-number v-model="dataForm.close_time" :min="3" :precision="0" :controls="false" :max="360"></el-input-number>
        <span>分钟（可输入3-360分钟）</span>
      </el-form-item>
      <el-form-item label="成团小时数" prop="limit_hour">
        <el-input-number v-model="dataForm.limit_hour" :min="1" :precision="0" :controls="false" :max="360"></el-input-number>
        <span>小时</span>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from '@/utils'
import Imglist from '../../oss/oss-uploadImg.vue'
import seviceApi from '@/api/serviceApi.js'
export default {
  components: {
    Imglist
  },
  data () {
    return {
      visible: false,
      dialogVisible: false,
      dataForm: {},
      dataRule: {
        name: [
          { required: true, message: '活动名称不能为空', trigger: 'blur' }
        ],
        start_time: [
          { required: true, message: '起止时间不能为空', trigger: 'blur' }
        ],
        end_time: [
          { required: true, message: '起止时间不能为空', trigger: 'blur' }
        ],
        img_url: [
          {
            required: true,
            message: '活动图片不能为空',
            trigger: 'blur'
          }
        ],
        close_time: [
          {
            required: true,
            message: '订单自动关闭时间不能为空',
            trigger: 'blur'
          }
        ],
        limit_hour: [
          {
            required: true,
            message: '成团小时数不能为空',
            trigger: 'blur'
          }
        ]
      },
      imgArr: []
    }
  },

  methods: {
    init (id) {
      this.resetData()
      this.dataForm.id = id || null
      this.visible = true

      if (id) {
        seviceApi.getGroupbuyActivityInfo(id).then((res) => {
          if (res && res.data.code == 200) {
            this.dataForm = res.data.data
            this.imgArr = this.dataForm.img_url.split(';')
          }
        })
      }
    },
    resetData () {
      (this.dataForm = {
        name: '', // 拼团活动名称
        start_time: '', // 活动开始时间
        end_time: '', // 活动结束时间
        img_url: '', // 活动图片
        explain: '', // 活动说明
        limit_hour: '', // 成团小时数(开团之后的小时数)
        close_time: '' // 订单自动关闭时间（商品按下单减库存，请设置未付款订单自动取消时间及时释放库存，可输入3-360分钟）
      }),
        this.resetForm('dataForm')
      this.imgArr = []
      this.timeQuantum = ''
    },
    // 上传图片
    updataImg (e) {
      this.dataForm.img_url = e
      console.log(e)
      this.$refs['dataForm'].clearValidate(['img_url'])
    },
    // 提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (new Date(this.dataForm.end_time).getTime() - new Date(this.dataForm.start_time).getTime() < 0) {
            this.$message({
              showClose: true,
              message: '结束时间不能小于开始时间',
              type: 'error'
            })
            return false
          }
          if (
            Object.prototype.toString.call(this.dataForm.img_url) ===
            '[object Array]'
          ) {
            this.dataForm.img_url = this.dataForm.img_url.join(',')
          } else {
            this.dataForm.img_url = this.dataForm.img_url
          }
          if (this.dataForm.id == null) {
            delete (this.dataForm.id)
            seviceApi.saveGroupbuyActivity(this.dataForm).then((res) => {
              if (res && res.data.code == 200) {
                this.$message({
                  showClose: true,
                  message: '添加成功',
                  type: 'success'
                })
                this.visible = false
                this.$emit('updataList', 1)
              }
            })
          } else {
            seviceApi.updateGroupbuyActivity(this.dataForm).then((res) => {
              if (res && res.data.code == 200) {
                this.$message({
                  showClose: true,
                  message: '修改成功',
                  type: 'success'
                })
                this.visible = false
                this.$emit('updataList', 1)
              }
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.date-lable /deep/ .el-form-item__content {
  display: flex;
  justify-content: flex-start;
}
</style>
